import time
from hypium import BY
from hypium.model import MatchPattern, UiParam
from devicetest.core.test_case import TestCase, Step, CheckPoint
from hypium import *

# 类名 要跟文件名保持一致！
class JiaoYinShiLuoDe_007(TestCase):
    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)
        self.driver = UiDriver(self.device1)

    def setup(self):
        Step('1.回到桌面')
        self.driver.swipe_to_home()
        # self.driver.swipe(direction="DOWN", distance=100)
        # self.driver.swipe(direction="UP", distance=80)
        # self.driver.swipe(direction="UP", distance=80)


    # 打开“交银基金”App,断网且非首次打开
    def process(self):
        Step('测试步骤1：打开“交银基金”APP,下拉状态栏')
        self.driver.start_app(package_name="com.fund001.etradingapp.hos")
        self.driver.find_component(BY.text("刷新"))
        self.driver.touch(BY.text("刷新"))

        # ========== 新增 CheckPoint 验证：断网时弹窗：“您的网络连接已断开，请检查手机网络” ==========
        CheckPoint('验证：断网时弹窗：“您的网络连接已断开，请检查手机网络”')
        alter_btn = self.driver.find_component(BY.text("您的网络连接已断开，请检查手机网络"))
        assert alter_btn is not None, "没有弹窗：您的网络连接已断开，请检查手机网络"

        Step('测试步骤2：上下滑动，打开WLAN')
        self.driver.swipe(side=UiParam.TOP,direction="DOWN",distance=100,start_point=(0.8, 0.0))  # 右上角顶部区域下滑
        time.sleep(1)
        # 打开WiFi
        self.driver.find_component(BY.text("WLAN"))
        self.driver.touch(BY.text("WLAN"))
        # 上划
        self.driver.swipe(direction="UP", distance=80)
        self.driver.swipe(direction="UP", distance=80)
        time.sleep(1)

        Step('测试步骤3：再次点击：刷新')
        x3 = self.driver.find_component(BY.text("刷新"))
        x3 = self.driver.touch(BY.text("刷新"))
        # ========== 新增 CheckPoint 验证：刷新成功，返回广告页并进入app首页=========
        # 这里需要等待5s才能进入到首页，因为有广告
        time.sleep(5)
        CheckPoint('进入App首页')
        home_page_btn = self.driver.find_component(BY.text("首页"))
        assert home_page_btn is not None, "未找到“首页”按钮"

    def teardown(self):
        Step('关闭交银基金APP')
        time.sleep(3)
        self.driver.stop_app("com.fund001.etradingapp.hos")